package com.rentdvd.client.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.rentdvd.ws.RentDVDImplProxy;

@WebServlet("/rent")
public class RentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public RentServlet() {
		super();
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String userName = (String) request.getSession().getAttribute("username");
		int[] filmList = new int[]{};
		
		String requestString = request.getParameter("data");
		try {
			String[] idArray = requestString.split(",");
			
			filmList = new int[idArray.length];
			
			for (int i = 0; i < idArray.length; i++) {
				filmList[i] = Integer.parseInt(idArray[i]);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		RentDVDImplProxy service = new RentDVDImplProxy();
		
		if (filmList.length != 0) {
			service.addWatchedFilms(userName, filmList);
		}
	}
}